package com.sxf.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import com.sxf.dao.CommentDAO;
import com.sxf.domain.Comment;
import com.sxf.domain.CommentMeta;
import com.sxf.domain.CommentVO;
import com.sxf.mybatis.page.Pagination;
import com.sxf.querybean.CommentQuery;

public class CommentDAOImpl extends SuperDAOImpl implements CommentDAO {

	private static final String STATEMENT_QUERY = "mybatis.xml.Comment.query";

	@Override
	public Pagination<CommentVO> queryPage(CommentQuery query) {
		return queryPageTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public List<CommentVO> queryList(CommentQuery query) {
		return queryListTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public CommentVO queryOne(CommentQuery query) {
		return queryOneTemplate(STATEMENT_QUERY, query);
	}

	@Override
	public long queryNumber(CommentQuery query) {
		return queryNumberTemplate(STATEMENT_QUERY, query);
	}

	public Map<String, Integer> queryStatistic() {
		String sql = "SELECT t.approved,count(t.approved) `count` from wp_comments t GROUP BY t.approved";
		Map<String, Integer> map = jt.query(sql,
				new ResultSetExtractor<Map<String, Integer>>() {
					@Override
					public Map<String, Integer> extractData(ResultSet rs)
							throws SQLException, DataAccessException {
						Map<String, Integer> map = new HashMap<String, Integer>();
						while (rs.next()) {
							map.put(rs.getString(1), rs.getInt(2));
						}
						return map;
					}
				});
		return map;
	}

	@Override
	public int insert(Comment info) {
		// sf.openSession().save(comment);
		if (info != null) {
			int i = st.insert("mybatis.xml.Comment.insertSelective", info);
			return i;
		}
		return 0;
	}

	@Override
	public int update(Comment info) {
		// sf.openSession().update(comment);
		if (info != null) {
			return st.update("mybatis.xml.Comment.updateByPrimaryKeySelective",
					info);
		}
		return 0;
	}

	@Override
	public int delete(Comment info) {
		// sf.openSession().delete(comment);
		if (info != null) {
			return st.delete("mybatis.xml.Comment.deleteByPrimaryKey", info);
		}
		return 0;
	}

	@Override
	public long insertCommentMeta(CommentMeta info) {
		if (info != null) {
			int i = st.insert("mybatis.xml.CommentMeta.insertSelective", info);
			return i;
		}
		return 0;
	}

	@Override
	public int updateCommentMeta(CommentMeta info) {
		if (info != null) {
			return st
					.update("mybatis.xml.CommentMeta.updateByPrimaryKeySelective",
							info);
		}
		return 0;
	}

	@Override
	public int deleteCommentMeta(CommentMeta info) {
		if (info != null
				&& (info.getId() != null || info.getCommentId() != null)) {
			return st
					.delete("mybatis.xml.CommentMeta.deleteByPrimaryKey", info);
		}
		return 0;
	}
}
